package com.iwhalecloud.bss.kite.cucc.order.loader.impl;

import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInst;
import com.iwhalecloud.bss.kite.dataservice.mapper.manager.KiteProdInstMapper;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.order.client.dto.InstContainer;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 接入产品实例加载器
 */
@Component("orderProdInstLoader")
public class ProdInstLoader extends InstLoader<ProdInst> {

    @Resource
    private KiteProdInstMapper kiteProdInstMapper;

    @Override
    public ProdInst getInst(String tableName, String prodInstId, String custId) {
        ProdInst prodInst = kiteProdInstMapper.getProdInstInMainOfferByProdInstId(prodInstId, custId);
        if (prodInst == null) {
//            prodInst = kiteProdInstMapper.getProdInstInBaseOfferByProdInstId(prodInstId, custId);
        }

        ProdInst newProdInst = KiteObjectUtils.clone(prodInst);
        if (Objects.nonNull(newProdInst) && Objects.equals(ActionType.A.name(), newProdInst.getActionType())) {
            newProdInst.setNewActionType(ActionType.K.name());
        }
        return newProdInst;
    }

    public ProdInst loadInstByCompInstId(OrderRequest orderRequest, String compInstId,
        String custId) {
        ProdInst prodInst = orderRequest.getInstContainer().getProdInstMap().get(compInstId);
        if (Objects.isNull(prodInst)) {
            prodInst = kiteProdInstMapper.getProdInstByCompInstId(compInstId, custId);
            InstContainer.ofRequest(orderRequest).addProdInst(prodInst);
        }

        return prodInst;
    }
}
